package com.atguigu.flink.chapter11;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @Author lzc
 * @Date 2022/10/31 10:41
 */
public class Flink08_SQL_UpsertKafka {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);
        
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        
        // 建立一张表与kafka 的 topic 关联: ddl(建表语句)
        tEnv.executeSql("create table sensor(" +
                            "id string, " +
                            "ts bigint, " +
                            "vc int" +
                            ")with(" +
                            "  'connector' = 'kafka', " +
                            "  'topic' = 's1', " +
                            "  'properties.bootstrap.servers' = 'hadoop162:9092', " +
                            "  'properties.group.id' = 'testGroup', " +
                            "  'scan.startup.mode' = 'latest-offset', " +
                            "  'format' = 'csv'" +
                            ")");
        
        Table result = tEnv.sqlQuery("select id, sum(vc) vc_sum  from sensor group by id");
    
        tEnv.executeSql("create table abc(" +
                            "id string, " +
                            "vc_sum int, " +
                            // flink本身没有自己的数据, 所以不能对数据的主键做非空唯一的交易
                            "primary key (id) NOT ENFORCED" +  // 唯一的目的: 是确定写入到 kafka 的时候的 key
                            ")with(" +
                            "  'connector' = 'upsert-kafka', " +
                            "  'topic' = 'sa', " +
                            "  'properties.bootstrap.servers' = 'hadoop162:9092', " +
                            "  'key.format' = 'json', " +
                            "  'value.format' = 'json'" +
                            ")");
    
        result.executeInsert("abc");
        
        
    }
}
/*
普通的 kafka 连接器只支持写入 append only 数据, 不能写入更新数据

 */